window.addEventListener('load', function () {
    /* 局部导航栏图片 */
    var local_nav_ico = document.querySelectorAll('.local_nav_ico');

    for (var i = 0; i < local_nav_ico.length; i++) {
        local_nav_ico[i].style.backgroundPosition = '0 -' + i * 32 + 'px';
    }


    //focus轮播图
    var focus = document.querySelector('.focus');
    var ul = focus.children[0];
    //获得focus 的宽度
    var w = focus.offsetWidth;
    //获取小圆点
    var ol = focus.children[1];

    var flag = false;

    var index = 0;
    var timer = setInterval(function () {
        index++;
        var translatex = -index * w;
        ul.style.transition = 'all .3s'
        ul.style.transform = 'translateX(' + translatex + 'px)'
    }, 3000);

    //过渡完成之后，再去拍段 监听过渡时间 transitonend
    ul.addEventListener('transitionend', function () {

        if (index >= 3) {
            index = 0;
            ul.style.transition = 'none';
            translatex = -index * w;
            ul.style.transform = 'translateX(' + translatex + 'px)'
        } else if (index < 0) {
            index = 2;
            ul.style.transition = 'none';
            translatex = -index * w;
        }

        //小圆点跟随变化
        ol.querySelector('.current').classList.remove('current');
        ol.children[index].classList.add('current');


    });

    //手指滑动轮播图
    var startX = 0;
    var moveX = 0;

    ul.addEventListener('touchstart', function (e) {
        startX = e.targetTouches[0].pageX;
        clearInterval(timer);

    });

    ul.addEventListener('touchmove', function (e) {
        moveX = e.targetTouches[0].pageX - startX;

        var translateX = -index * w + moveX;
        ul.style.transition = 'none';
        ul.style.transform = 'translateX(' + translateX + 'px)';
        e.preventDefault();

        // flag = true;
    });

    ul.addEventListener('touchend', function (e) {
        if (Math.abs(moveX) > 50) {
            //如果右滑播放上一张 moveX 是正值
            //如果左滑播放下一张 moveX 是负值
        //    if(flag){
            if (moveX > 0) {
                index--;
            } else {
                index++;
            }
            translateX = -index * w;
            ul.style.transition = 'all .3s';
            ul.style.transform = 'translateX(' + translateX + 'px)';
        }else{
            translateX = -index * w;
            ul.style.transition = 'all .3s';
            ul.style.transform = 'translateX(' + translateX + 'px)';
        }
        //    }

        clearInterval(timer);
        timer = setInterval(function () {
            index++;
            var translatex = -index * w;
            ul.style.transition = 'all .3s'
            ul.style.transform = 'translateX(' + translatex + 'px)'
        }, 3000);

        // flag = false;
    });

    //返回顶部
    var goback = document.querySelector('.goback');
    var nav = document.querySelector('nav');
    window.addEventListener('scroll',function(){
        if(window.pageYOffset >= nav.offsetTop){
            goback.style.display = 'block';
        }else{
            goback.style.display = 'none';
        }
    })

    goback.addEventListener('click',function(){
        window.scroll(0,0);
    })

})